import { reactive, ref } from "vue";
import { GJ_nowTime } from "../../../static/js/tool.js";
import router from "../../router/index.js";
import { Api } from "../../api/api.js";
import { ElNotification } from "element-plus";
function luckMeetJS() {
    let userinfo = ref(JSON.parse(sessionStorage.getItem("userinfo")));
    let status = reactive({
        // 头衔
        honoy: [
            {
                arrive: 0,
                name: "脱单成功",
                style: {
                    border: "1px solid yellow",
                    color: "yellow",
                    padding: "3px 6px",
                    marginBottom: "2px",
                    marginLeft: "10px",
                    fontSize: "12px",
                },
                desc: "结缘成功后获得"
            },
            {
                arrive: 100,
                name: "一见倾心",
                style: {
                    border: "1px solid pink",
                    color: "pink",
                    padding: "3px 6px",
                    marginBottom: "2px",
                    marginLeft: "10px",
                    borderRadius: "2px",
                    fontSize: "12px",
                },
                desc: "情缘值总和超过100获得"
            },
            {
                arrive: 500,
                name: "天作之合",
                style: {
                    border: "1px solid #FF3D64",
                    color: "#FF3D64",
                    padding: "3px 6px",
                    marginBottom: "2px",
                    marginLeft: "10px",
                    borderRadius: "2px",
                    fontSize: "12px",
                },
                desc: "情缘值总和超过500获得"
            },

        ],
        // 申请结缘信息
        meetData: {
            user: "",
            userName: "",
            toUser: "",
            promise: "",
            createTime: "",
        },
        //已结缘信息
        luckData: [],
        //   未结缘信息
        reqList: []
    });
    let methodFun = {
        //回到首页
        goHome: () => {
            router.push("/overview/summary");
        },
        //结缘状态
        parseStatus: (val) => {
            if (val == 1) {
                return "等待查看"
            } else if (val == 2) {
                return "对方考虑中"
            } else if (val == 3) {
                return "对方已接受"
            } else if (val == 4) {
                return "对方已拒绝"
            }
        },
        //   撤回结缘
        delLove: (item) => {
            item.status = 5;
            item.createTime = GJ_nowTime("date-time", "-");
            Api.delLove(item).then(() => {
                methodFun.getLove();
            })
        },
        //   申请结缘
        submitLuck: () => {
            status.meetData.createTime = GJ_nowTime("date-time", "-");
            status.meetData.user = userinfo.value.user;
            status.meetData.userName = userinfo.value.userName;
            if (status.meetData.user != status.meetData.toUser) {
                Api.addLove(status.meetData).then((res) => {
                    if (res.data) {
                        methodFun.getLove();
                        ElNotification({
                            type: "success",
                            title: "操作成功",
                            message: "结缘申请已成功发送",
                            duration: 1000,
                        });
                    } else {
                        ElNotification({
                            type: "warning",
                            title: "对方已经结缘了、你来晚了哦",
                            message: "鲜花野草千千万、不信咱就换！",
                            duration: 1000,
                        });
                    }
                });
            } else {
                ElNotification({
                    type: "error",
                    title: "你在想什么呢",
                    message: "要对自己下手了？这不可以呦",
                    duration: 1000,
                });
            }

        },
        //获取结缘列表
        getLove: () => {
            let user = userinfo.value.user;
            let toUser = userinfo.value.toUser;
            let luckMeet = userinfo.value.luckMeet;
            if (luckMeet) {
                Api.getLove({ user, toUser }).then((res) => {
                    status.luckData.push(res.data[0]);
                    Api.userFind({ user: [user, toUser] }).then((userData) => {
                        status.luckData.push(...(userData.data.data));
                        
                    })
                })
            } else {
                Api.getLove({ user }).then((res) => {
                    status.reqList = res.data;
                });
            }


        },
        //等级
        gradeFilter(value) {
            return parseInt(value / 100);
        },
        //称号
        honoyFilter(val) {
            let honoyList = status.honoy.filter((item) => {
                return item.arrive <= val;
            })
            return honoyList
        },
        honoyHover(item, index) {
            if (!("textShadow" in status.honoy[index].style)) {
                let color = item.style.color;
                status.honoy[index].style.textShadow = `1px 1px 4px ${color}`;
                status.honoy[index].style.boxShadow = `0px 0px 4px ${color}`;
            }
        },
        honoyLeave(item, index) {
            delete status.honoy[index].style.boxShadow;
            delete status.honoy[index].style.textShadow;
        }
    };

    return {
        userinfo,
        status,
        methodFun
    }
}

export default luckMeetJS;